//
//  LCCKInputViewPluginPickImage.m
//  Pods
//
//  v0.8.5 Created by ElonChan on 16/8/11.
//
//

#import "LCCKInputViewPluginPickImage.h"
#import "HXPhotoPicker.h"

@interface LCCKInputViewPluginPickImage()<UIImagePickerControllerDelegate>

@property (nonatomic, copy) LCCKIdResultBlock sendCustomMessageHandler;
@property (nonatomic, copy) UIImagePickerController *pickerController;

@property(nonatomic,strong) HXPhotoManager *photoManage;
// 创建一个全局的group
@property (nonatomic, strong) dispatch_group_t group;

@end

@implementation LCCKInputViewPluginPickImage
@synthesize inputViewRef = _inputViewRef;
@synthesize sendCustomMessageHandler = _sendCustomMessageHandler;

#pragma mark -
#pragma mark - LCCKInputViewPluginSubclassing Method

+ (LCCKInputViewPluginType)classPluginType {
    return LCCKInputViewPluginTypePickImage;
}

#pragma mark -
#pragma mark - LCCKInputViewPluginDelegate Method

/*!
 * 插件图标
 */
- (UIImage *)pluginIconImage {
    return IMAGE_NAMED(@"open_img_picker");
//    return [self imageInBundlePathForImageName:@"chat_bar_icons_pic"];
}

/*!
 * 插件名称
 */
- (NSString *)pluginTitle {
    return @"图片";
}

/*!
 * 插件对应的 view，会被加载到 inputView 上
 */
- (UIView *)pluginContentView {
    return nil;
}

- (void)pluginDidClicked {
    [super pluginDidClicked];
    
    //显示相册
    [self selectPhotoHandler];
}

- (void)selectPhotoHandler {
    [self.photoManage clearSelectedList];
    HXCustomNavigationController *nav = [[HXCustomNavigationController alloc] initWithManager:self.photoManage doneBlock:^(NSArray<HXPhotoModel *> * _Nullable allList, NSArray<HXPhotoModel *> * _Nullable photoList, NSArray<HXPhotoModel *> * _Nullable videoList, BOOL original, UIViewController * _Nullable viewController, HXPhotoManager * _Nullable manager) {
        
        NSMutableArray *arr = @[].mutableCopy;
        
        
        dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
        dispatch_async(queue, ^{
            self.group = dispatch_group_create();
            
            for (HXPhotoModel *item in photoList) {
                dispatch_group_enter(self.group);

                [item getAssetURLWithSuccess:^(NSURL * _Nullable URL, HXPhotoModelMediaSubType mediaType, BOOL isNetwork, HXPhotoModel * _Nullable model) {
                    HEIMImageObject *image = [[HEIMImageObject alloc] init];
                    image.url = URL;
                    image.imageSourceType = HEIMImageSourceTypeNative;
                    [arr addObject: image];
                    dispatch_group_leave(self.group);

                } failed:^(NSDictionary * _Nullable info, HXPhotoModel * _Nullable model) {
                    dispatch_group_leave(self.group);
                }];
            }
            
            //因上面请求中有加入dispatch_group_enter 和 dispatch_group_leave,所以真正等待上面线程执行完才执行这里面的请求
            KWeakSelf
            dispatch_group_notify(self.group, queue, ^{
                
                if ([weakSelf.inputViewRef.delegate respondsToSelector:@selector(sendMessage:)]) {
                    HEIMImageObject *image = [[HEIMImageObject alloc] init];
                    image.imageArray = arr;

                    HEIMMessage *message = [[HEIMMessage alloc] init];
                    message.modelType = weakSelf.inputViewRef.type;
                    message.type = HEIMMessageTypeImage;
                    message.messageObject = image;
                    message.from = weakSelf.inputViewRef.userInfo.id;
                    message.actor = HEActorTypeUser;
                    message.to = weakSelf.inputViewRef.session.sessionId;
                    HEIMMessageModel *model = [[HEIMMessageModel alloc] initWithMessage: message];
                    model.deliveryState = HEIMMessageDeliveryStateDelivering;

                    [weakSelf.inputViewRef.delegate sendMessage: model];
                }
            });
        });
    } cancelBlock:^(UIViewController * _Nullable viewController, HXPhotoManager * _Nullable manager) {
        
    }];
    
    [self.conversationViewController presentViewController:nav animated:YES completion:nil];
}


#pragma mark -
#pragma mark - Private Methods
- (UIImage *)imageInBundlePathForImageName:(NSString *)imageName {
    UIImage *image = [UIImage wo_imageNamed:imageName bundleName:@"ChatKeyboard" bundleForClass:[self class]];
    return image;
}


#pragma mark - lazy

- (HXPhotoManager *)photoManage {
    if (!_photoManage) {
        _photoManage = [[HXPhotoManager alloc] initWithType:HXPhotoManagerSelectedTypePhoto];
        _photoManage.configuration.type = HXConfigurationTypeWXMoment;
        _photoManage.configuration.openCamera = YES;
        _photoManage.configuration.photoMaxNum = 9;
        _photoManage.configuration.videoMaxNum = 0;
        _photoManage.configuration.maxNum = 9;
        _photoManage.configuration.lookLivePhoto = YES;
        _photoManage.configuration.selectTogether = NO;
        _photoManage.configuration.deleteTemporaryPhoto = YES;
        _photoManage.configuration.creationDateSort = YES;
    }
    return _photoManage;
}

@end
